package com.boot.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.boot.entity.Report;
import com.boot.service.ReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;


/**
 * @Description: 首页控制器
 * @Author: HuangX
 * @Date: 2021/3/3
 */
@Slf4j
@Api(tags = "首页控制器")
@RestController
@SuppressWarnings({"SpringJavaInjectionPointsAutowiringInspection"})
public class IndexController {

    @Autowired
    private ReportService reportService;

    @ApiOperation("测试方法")
    @GetMapping("/api/index")
    public String getIndex(){
        StpUtil.setLoginId(10001);
//        IdWorker.getId()
//        userService.getUser();
        log.info("+++++++++getIndex异步方法开始执行+++++++");
        StpUtil.checkLogin();
        return "ok";
    }

    @PostMapping("/api/upload")
    public String upload(@RequestParam("file") MultipartFile file) throws Exception{
        long stater = System.currentTimeMillis();
        List<Report> data = new ArrayList<>();
//        ExcelReader reader = new ExcelReader(file.getInputStream(),0);
//        List<Report> read = reader.read(0, 1, Report.class);
//        List<Report> reports = reader.readAll(Report.class);
//        List<List<Object>> read = reader.read()
//        data = reader.read(0, 0, Report.class);
        EasyExcel.read(file.getInputStream(), Report.class, new AnalysisEventListener<Report>() {
            @Override
            public void invoke(Report row, AnalysisContext context) {
                data.add(row);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                log.info("导入完成");
            }
        }).sheet().doRead();
        long end = System.currentTimeMillis();
        log.info("导入用时"+(end-stater));
        reportService.saveBatch(data);

        log.info("导入用时"+(end-stater));
        return "success";
    }
}
